﻿// https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
// 111. 二叉树的最小深度

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <limits.h>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int minDepth(TreeNode* root) {
        return GetDepth(root);
    }

    int GetDepth(TreeNode *root) {
        if (root == nullptr) 
            return 0;
        int left_depth = GetDepth(root->left);
        int right_depth = GetDepth(root->right);

        if (root->left == nullptr && root->right != nullptr) {
            return 1 + right_depth;
        }
        if (root->left != nullptr && root->right == nullptr) {
            return 1 + left_depth;
        }
        int result = 1 + min(left_depth, right_depth);
        return result;
    }
};

int main() {
    Solution obj = Solution();
}